// pages/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: false,
    phone:false,
    phoneshow:false,
    loginShow: false,
    phone:false,
    tabActive: 1,
    refresh: true,
    search: {
      pageNumber: 1,
      pageSize: 10,
      totalPages: 0
    },
    list: [],
    userInfo: {},
    req: false
  },
  login() {
    var that = this
    if(that.data.login){
      that.toggle('phoneshow', true)
      that.selectComponent('#phone').showClose()
    } else {
      that.toggle('loginShow', true)
      that.selectComponent('#login').showClose()
    }
  },
  // loginClose() {
  //   this.toggle('loginShow', false)
  // },
  // onClose() {
  //   this.toggle('phoneshow', false)
  // },
  toggleToast(e) {
    // console.log(e.detail)
    if (!e.detail.loginShow) {
      this.toggle('loginShow', false)
    }
    this.getuserInfo()
  },
  toggleToast1(e) {
    // console.log(e.detail)
    if (!e.detail.phoneshow) {
      this.toggle('phoneshow', false)
    }
    this.getuserInfo()
  },
  onChange(e) {
    var index = e.detail.index;
    this.toggle("tabActive", index);
    wx.setNavigationBarTitle({
      title: e.detail.title
    })
    this.refreshList();
  },
  bindchange() {},
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    this.toggle('refresh', false)
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    if (url =='/pages/creat/creat'){
      this.toggle('refresh', true);
      getApp().mtj.trackEvent('yqlw_org_create');
    } else if (url == '/pages/orgHome/orgHome' && this.data.tabActive==2){
      getApp().mtj.trackEvent('yqlw_org_recommend');
    } else if (url == '/pages/orgHome/orgHome' && this.data.tabActive == 1){
      this.toggle('refresh', true)
    }
    if (dataset.id) {
      url = url + "?id=" + dataset.id
    }
    wx.navigateTo({
      url: url
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (options.tabActive==1){
      this.toggle("tabActive", 2);
    }
  },
  refreshList: function() {
    this.toggle("req", false);
    this.toggle("search.pageNumber", 1);
    this.toggle("search.total", 0);
    this.toggle("search.totalPages", 0);
    this.toggle("list", []);
    this.list();
  },
  existMyOrg: function(callback) {
    var that = this;
    var userInfo = that.data.userInfo;
    that.toggle("tabActive", userInfo && userInfo.userOrgList && userInfo.userOrgList.length > 0 ? 1 : 2);
    if (that.data.tabActive == 1) {
      wx.setNavigationBarTitle({
        title: '我的组织'
      })
    }
    callback && callback();
  },
  list: function(append) {
    var that = this;
    var tabActive = that.data.tabActive;
    if (0 == tabActive) {
      return;
    }
    var url = 1 == tabActive ? "orgmini/org/newGetOrgList" : "orgmini/org/recommendList";
    var titleLength = 1 == tabActive ? 8 : 14;
    app.req({
      url: url,
      data: that.data.search,
      success: function(res) {
        if (200 == res.code) {
          var list = that.data.list;
          var nowList = res.data.rows;
          // for (var item of nowList) {
          //   item.orgName = item.orgName.length >= titleLength ? item.orgName.substr(0, titleLength - 2) + "..." : item.orgName;
          // }
          that.toggle("list", append ? list.concat(nowList) : nowList);
          that.toggle("search.totalPages", res.data.totalPages * 1);
          that.toggle("req", true);
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    that.getuserInfo()
  },
  getuserInfo() {
    var that = this
    wx.getSetting({
      success(res) {
        // console.log(res)
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if (wx.getStorageSync('uid') == '') {
          that.toggle("login", false);
          that.toggle("search.pageNumber", 1);
          that.toggle("search.pageSize", 10);
          that.toggle("search.totalPages", 0);
          if (that.data.refresh) {
            that.existMyOrg(that.refreshList);
          }
        } else {
          that.toggle("login", true);
          var phone = wx.getStorageSync("phone")
          if (phone == '') {
            that.toggle("phone", true);
          }else{
          that.toggle("phone", false);
          that.toggle("phoneshow", false);
          that.toggle("loginShow", false);
          app.getUserInfo(function(userInfo) {
            that.toggle("search.uid", userInfo.id);
            that.toggle("search.pageNumber", 1);
            that.toggle("search.pageSize", 10);
            that.toggle("search.totalPages", 0);
            that.toggle("userInfo", userInfo);
            if (that.data.refresh) {
              that.existMyOrg(that.refreshList);
            }
          });
        }
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if (wx.getStorageSync('uid') == '') {
          that.refreshList();
        } else {
          app.getUserInfo(function(userInfo) {
            that.toggle("search.uid", userInfo.id);
            that.toggle("userInfo", userInfo);
            that.refreshList();
          });
        }
      },
    })

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    // if (that.data.tabActive != 2) {
      var search = that.data.search;
      if (search.totalPages > search.pageNumber) {
        that.toggle("search.pageNumber", search.pageNumber + 1);
        that.list(true);
      }
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})